package com.bianguo.android.tencentpushdemo.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.adapters.ServiceAdapter;
import com.bianguo.android.tencentpushdemo.events.BaseEvent;
import com.bianguo.android.tencentpushdemo.view.widget.TopStoryPager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

public class ServiceFragment extends BaseFragment implements OnRefreshListener {

    @BindView(R.id.fragment_service_text)
    TextView text;
    @BindView(R.id.fragment_service_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.fragment_service_banner)
    TopStoryPager mBannerPager;
    @BindView(R.id.fragment_service_recycler)
    RecyclerView serviceRecycler;

    private int type;
    private ArrayList<String> datas;
    private ServiceAdapter serviceAdapter;
    private LinearLayoutManager layoutManager;

    public static ServiceFragment newInstance(int type) {
        ServiceFragment fragment = new ServiceFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_service;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        type = bundle.getInt("type");
        switch (type) {
            case 1:
                text.setText("ServiceFragment");
                break;
            case 2:
                text.setText("FindFragment");
                break;
            case 3:
                text.setText("MeFragment");
                break;
        }
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        mRefreshLayout.setEnableLoadmore(false);
        mRefreshLayout.setOnRefreshListener(this);
        ArrayList<String> list = new ArrayList<>();
        list.add("http://demo.bianguo.com.cn/zhuzhenyu/scnews/Uploads/avatar/2017-05-23/5923fdd3c7358.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468555507&di=268ceae910b98448da3cdd6e119dadb4&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01c54b554113d9000001e71b1de404.jpg%401280w_1l_2o_100sh.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468610182&di=6b7980ab81fc4a44b3dacb65557d6a76&imgtype=0&src=http%3A%2F%2Fimg.ivsky.com%2Fimg%2Fbizhi%2Fpre%2F201511%2F10%2Fmercedes_benz-013.jpg");
        list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529468654167&di=c8188ba303bb6d48ecb7d166886d1195&imgtype=0&src=http%3A%2F%2Fimg.ieche.com%2F1%2F1199%2F200912%2F20091226175124337.jpg");
        mBannerPager.init(list);
        mBannerPager.startTimer(2000);
        datas = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            datas.add("item ---> " + i);
        }
        serviceAdapter = new ServiceAdapter(getActivity(), datas);
        layoutManager = new LinearLayoutManager(getActivity());
        serviceRecycler.setLayoutManager(layoutManager);
        serviceRecycler.setAdapter(serviceAdapter);
    }

    @Override
    public void onEvent(BaseEvent event) {
        super.onEvent(event);
        text.setTextSize(24.0f);
        text.setTextColor(Color.BLUE);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.finishRefresh(300);
            }
        }, 1500);
    }
}
